package com.example.sports_meet;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

public class RegisterActivity extends AppCompatActivity {
    public static final int RESULT_CODE_REGISTER = 0;
    private Button btnRegister;
    private EditText etAccount, etPass, etPassConfirm;
    private CheckBox cbAgree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register); //使用的界面文件
        Objects.requireNonNull(getSupportActionBar()).hide();


        //diyig删除按钮
        etAccount = findViewById(R.id.et_account);
        ImageButton deleteOneButton = findViewById(R.id.delete_one);

        deleteOneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etAccount.setText("");
            }
        });

        //第二个删除按钮
        etPass = findViewById(R.id.et_password);
        ImageButton deleteSecButton = findViewById(R.id.delete_second);

        deleteSecButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etPass.setText("");
            }
        });


        //第三个删除按钮
        etPassConfirm = findViewById(R.id.et_password_Confirm);
        ImageButton deleteThirdButton = findViewById(R.id.delete_third);

        deleteThirdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etPassConfirm.setText("");
            }
        });

        //获取控件
        etAccount = findViewById(R.id.et_account);
        etPass = findViewById(R.id.et_password);
        etPassConfirm = findViewById(R.id.et_password_Confirm);
        btnRegister = findViewById(R.id.btn_register);

        //注册按钮设置响应事件
        btnRegister.setOnClickListener(this::onClick);

    }


    //@Override
    public void onClick(View v) {
        String name = etAccount.getText().toString();
        String pass = etPass.getText().toString();
        String PassConfirm = etPassConfirm.getText().toString();
        //如果用户名name值为空
        if (TextUtils.isEmpty(name)) {

            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //如果密码pass值为空
        if (TextUtils.isEmpty(pass)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //如果pass和PassConfirm值不一样
        if (!TextUtils.equals(pass, PassConfirm)) {

            Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }


        //存储注册的用户名和密码
        SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
        SharedPreferences.Editor edit = spf.edit();
        edit.putString("account", name);
        edit.putString("password", pass);
        //注册成功后，回到登录界面，数据回传
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("account", name);
        bundle.putString("password", pass);
        intent.putExtras(bundle);
        setResult(RESULT_CODE_REGISTER, intent);
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        this.finish();


    }
}